
#include "include.h"

/* Implementation file */
NS_IMPL_ISUPPORTS1(PythonMessageReceiver, nsIMessageReceiver);

PythonMessageReceiver::PythonMessageReceiver(PyObject* pFunc)
{
	m_pFunc = pFunc;
}

PythonMessageReceiver::~PythonMessageReceiver()
{
	Py_XDECREF(m_pFunc);
}

extern PyThreadState* mainThread;

/* void writeValue (in wstring title, in wstring data); */
NS_IMETHODIMP PythonMessageReceiver::WriteValue(const PRUnichar *title, const PRUnichar *data)
{
	PyEval_RestoreThread(mainThread);

	nsresult nv = NS_OK;

	PyObject* pArgs = PyTuple_New(2);

	PyTuple_SetItem(pArgs, 0, PRUnicharToPyObject(title));
	PyTuple_SetItem(pArgs, 1, PRUnicharToPyObject(data));
	PyObject* result = PyObject_CallObject(m_pFunc, pArgs);
	if (result == NULL) {
		PyErr_Print();
		PyErr_Clear();
		nv = NS_ERROR_UNEXPECTED;
	}
	Py_XDECREF(result);
	Py_XDECREF(pArgs);

	mainThread = PyEval_SaveThread();

	return nv;
}
